<template>
  <div>
    <el-row type="flex" justify="center" align="middle" class="loginWrapper">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button @click="clearInput">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    clearInput() {
      this.form = {
        username: '',
        password: ''
      }
    },
    login() {
      if (!this.form.username || !this.form.password) {
        this.$message.error('请输入完整信息')
        return
      }
      this.$axios({
        url: '/login',
        method: 'post',
        data: this.form
      }).then(res => {
        // console.log(res.data)
        this.$message.success(res.data.message)

        localStorage.setItem('token', res.data.data.token)
        localStorage.setItem('user', JSON.stringify(res.data.data.user))

        this.$router.push('/')
      })
    }
  }
}
</script>
<style lang="less" scoped>
.loginWrapper {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
}
</style>
